Questions
17 of 45
1What is a pseudo-element in CSS?
2How are pseudo-elements different from pseudo-classes?
3What is the syntax of a pseudo-element?
4Name some commonly used pseudo-elements.
5What does the ::before pseudo-element do? What does the ::after pseudo-element do?
6How can you insert content using pseudo-elements?
7What is the difference between :before and ::before? Why were pseudo-elements changed from one colon (:) to two (::) in CSS3?
8What does the ::first-letter pseudo-element do?
9What does the ::first-line pseudo-element do?
10How can you style the first line of a paragraph differently?
11Can pseudo-elements be styled with animations or transitions?
12What’s the difference between using ::before and ::after?
13Can pseudo-elements be positioned using CSS positioning properties? Can pseudo-elements have background images or colors? How can you use pseudo-elements to create shapes or icons?
14Can you use multiple pseudo-elements on the same element?
15What is the ::selection pseudo-element used for?
16How does stacking and z-index affect pseudo-elements?
17Can pseudo-elements have child elements or content inside them?
18Can you apply pseudo-elements to replaced elements (like <img>)?
19How do pseudo-elements interact with display and overflow properties?
20What’s the difference between using pseudo-elements and real elements for decoration?
21How does inheritance work with pseudo-elements?
22Can pseudo-elements trigger JavaScript events (like click)?
23How can you control the generated content with pseudo-elements using attr()?
24Can pseudo-elements be used inside flex or grid layouts?
25How do you control the stacking order of ::before and ::after pseudo-elements?
26How can you create a tooltip using pseudo-elements?
27How can you make a custom underline or highlight effect using ::after?
28How can you make a quotation mark appear before a paragraph using pseudo-elements?
29How do you create a triangle or arrow shape using only CSS pseudo-elements?
30How can you use pseudo-elements to add icons without extra markup?
31How can you create a button hover animation using ::before or ::after?
32How can pseudo-elements help in implementing skeleton loaders or shimmer effects?
33How can you use ::before and ::after to clear floats?
34How can you create a border animation using pseudo-elements?
35How can pseudo-elements be used in responsive design?
36What is the ::marker pseudo-element and when is it used?
37What does the ::placeholder pseudo-element do?
38How does ::cue work with media elements?
39What is ::backdrop, and where is it used?
40What is the difference between ::file-selector-button and other input pseudo-elements?
41How does the ::part() pseudo-element work in Web Components?
42How is ::slotted() different from ::part() in shadow DOM styling?
43Can you style text highlights with pseudo-elements?
44How can ::selection be customized for accessibility and branding?
45What are the limitations of pseudo-elements compared to actual DOM elements?
17 / 45

Can pseudo-elements have child elements or content inside them?

Child Elements and Content in Pseudo-Elements

Pseudo-elements like ::before and ::after cannot contain real child elements. They are not part of the DOM and exist purely for styling purposes. You can only insert text or other content through the CSS content property.

Key Points
  1. 1

    Pseudo-elements cannot have nested HTML elements inside them.

  2. 2

    The only content they can hold is inserted via the CSS content property, which supports text, counters, or attr() values.

  3. 3

    They are ideal for decorative content, icons, or visual enhancements without altering the HTML structure.

  4. 4

    You can style them extensively with CSS properties like color, background, position, transform, and z-index, but not add real DOM children.

Example: Inserting Content Using ::before

In this example, the ::before pseudo-element inserts a star character before the paragraph text. You cannot insert another HTML element, only textual or generated content.

Best Practices
  1. 1

    Use pseudo-elements to add non-interactive decorative content.

  2. 2

    Do not attempt to insert real HTML elements; use content for text or symbols.

  3. 3

    Combine with CSS styling for effects like icons, badges, or overlays.

  4. 4

    Remember pseudo-elements are virtual and cannot respond directly to JavaScript events.

Difficulty: 3/10
Topics: pseudo-elements, CSS content model, DOM vs CSS rendering

Scenario Questions

0-2 years experience
  1. 1

    You're trying to add a star icon after a review rating using ::after, but you want to put a span inside it to style part of the icon differently — what happens, and how do you fix it?

  2. 2

    I wrote CSS with ::before { content: '<div>Warning</div>'; } and nothing shows up — why?

  3. 3

    How would you add a tooltip icon after a link using pseudo-elements if you can't nest HTML inside them?

2-5 years experience
  1. 1

    Our design system uses ::before for decorative badges on buttons, but now we need to make them focusable and keyboard-navigable — why is this impossible, and what’s your workaround?

  2. 2

    A teammate used ::after to inject a dynamic count badge, but it breaks when the content changes via JS — what’s the root cause, and how do you debug it?

  3. 3

    We’re seeing layout shifts when pseudo-element content changes on hover — is this related to how pseudo-elements render, and how do you stabilize it?

5-8 years experience
  1. 1

    We’re building a scalable component library and considering pseudo-elements for all decorative UI elements — what are the performance, accessibility, and maintainability tradeoffs compared to real DOM nodes?

  2. 2

    How would you design a responsive tooltip system that works across browsers and screen readers, given that pseudo-elements can't contain interactive children?

  3. 3

    A legacy component uses ::before to inject ARIA labels dynamically — now we’re migrating to a framework that manages DOM state, how do you refactor this without breaking accessibility?

8+ years experience
  1. 1

    Our design system has hundreds of components using pseudo-elements for icons and badges — we’re now scaling to support internationalization and dynamic content injection. What architectural changes would you propose to future-proof this without sacrificing performance or accessibility?

  2. 2

    We’re evaluating a CSS-in-JS migration and need to decide whether to replace all pseudo-element-generated content with real DOM nodes. What long-term maintenance, team onboarding, and accessibility risks do you weigh?

  3. 3

    A cross-team component library relies on ::after for visual indicators that are now required to be interactive — how do you drive consensus on a migration strategy that balances technical debt, UX, and legacy browser support?

Follow-up Questions

  • What happens if you try to append a div inside a ::before using JavaScript?
  • Why can't you style pseudo-elements like regular elements with child selectors?
  • How does this limitation affect accessibility when using pseudo-elements for icons?